package com.example.FirstBook.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import com.example.FirstBook.adapter.ImageViewpageAdapter;
import com.example.myapplication.R;

import java.util.ArrayList;
import java.util.List;

public class Food_soft_Activity extends AppCompatActivity implements ViewPager.OnPageChangeListener{

    private ImageView titleBackImage;
    private TextView titleText;
    private ImageView titleCamera;
    private ViewPager viewpage;
    private LinearLayout layoyt;
    int[] image = new int[]{R.mipmap.ab1, R.mipmap.ab2, R.mipmap.ab3, R.mipmap.ab4, R.mipmap.ab5};
    List<ImageView> list = new ArrayList<>();
    List<ImageView> iconslist = new ArrayList<>();
    ImageViewpageAdapter adapter;
    private TextView softShare;

    ImageView []points;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_soft_);
        initView();
        intData();
        //开启线程，进行轮播
        PollThread pollThread = new PollThread();
        pollThread.start();
        share();
    }
    /*
    点击分享
     */
    public void share(){
        softShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //系统自带的分享
                Intent intent=new Intent(Intent.ACTION_SEND);
                intent.setType("text/Type");
                String SoftInfo="邹老师的软件：健康饮食，了解如何搭配食物的重要性，让你变得更健康";
                intent.putExtra(Intent.EXTRA_TEXT,SoftInfo);
                startActivity(Intent.createChooser(intent,"邹老师的分享"));
            }
        });
    }
    /*
    初始化控件
     */
    private void initView() {
        titleBackImage = (ImageView) findViewById(R.id.title_back_image);
        titleText = (TextView) findViewById(R.id.title_text);
        titleCamera = (ImageView) findViewById(R.id.title_camera);
        viewpage = (ViewPager) findViewById(R.id.viewpage);
        layoyt = (LinearLayout) findViewById(R.id.layoyt);
        softShare = (TextView) findViewById(R.id.soft_share);
    }

    /*
    初始化适配器数据
     */
    public void intData() {
        for (int i = 0; i < image.length; i++) {
            ImageView iv = new ImageView(this);
            iv.setImageResource(image[i]);
            //在代码中写控件，需要手动设置宽高
            iv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));
            iv.setScaleType(ImageView.ScaleType.CENTER_CROP);//等比例放大
            list.add(iv);
            //动态生成指示器
            ImageView piv = new ImageView(this);
            piv.setImageResource(R.drawable.point_normal);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(30, 30);
            lp.rightMargin = 20;
            piv.setLayoutParams(lp);
            iconslist.add(piv);//添加到集合
            layoyt.addView(piv);//添加到布局
        }
        iconslist.get(0).setImageResource(R.drawable.point_select);
        adapter = new ImageViewpageAdapter(list);
        viewpage.setAdapter(adapter);
        //设置viewpage监听器，监听滑动位置
        viewpage.addOnPageChangeListener(this);
        points=new ImageView[image.length];

        for (int i = 0; i < points.length; i++) {
            points[i]=(ImageView)layoyt.getChildAt(i);
            points[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
        }

    }

    class PollThread extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        viewpage.setCurrentItem(viewpage.getCurrentItem() + 1);
                    }
                });
            }
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        for (int i = 0; i < iconslist.size(); i++) {
            iconslist.get(i).setImageResource(R.drawable.point_normal);
        }
        iconslist.get(position % list.size()).setImageResource(R.drawable.point_select);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
